package application.gui.workerWindow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import application.CommunicationModule.Communication;
import application.LoginAndAdministrationModule.Worker;
import application.parcelModule.Parcel;

public class ChangeParcelStatusPanel extends JPanel {

	public ChangeParcelStatusPanel(WorkerHomePage parent, Communication c) {
		setSize(600, 480);
		setLayout(null);
		this.parent = parent;
		this.db = c;
		init();
	}

	private void init() {
		parcelStatusList = new JComboBox();
		for (String s : db.getStatusList())
			parcelStatusList.addItem(s);
		deliveryInput = new JTextField();
		searchButton = new JButton("Szukaj przesyłki");
		deliveryDetails = new JTextArea();
		deliveryDetails.setEditable(true);
		confirm = new JButton("Zatwierdź");
		statusInfo = new JLabel("Status przesyłki");
		checkDeliveryInfo = new JLabel("Wpisz nr przesyłki");
		info = new JLabel("Wpisz numer przesyłki aby sprawdzić oraz zmienić status przesyłki");
		errorInfo = new JLabel();
		parcelStatusList.setVisible(false);
		deliveryDetails.setVisible(false);
		confirm.setVisible(false);
		statusInfo.setVisible(false);
		addComponent(info, 20, 15, 400, 30);
		addComponent(checkDeliveryInfo, 50, 100, 120, 30);
		addComponent(deliveryInput, 220, 100, 100, 30);
		addComponent(searchButton, 360, 100, 150, 30);
		addComponent(deliveryDetails, 50, 160, 200, 120);
		addComponent(confirm, 350, 220, 100, 40);
		addComponent(statusInfo, 280, 160, 150, 30);
		addComponent(parcelStatusList, 450, 160, 120, 30);
		addComponent(errorInfo, 20, 60, 500, 30);
		setListeners();
		worker = new Worker(parent.getLoggedInUser());
	}
	
	private void addComponent(JComponent comp, int x, int y, int width, int height){
		add(comp);
		comp.setBounds(x, y, width, height);
	}
	
	private void setListeners(){
		searchButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (deliveryInput.getText().equals("")){
					errorInfo.setText("Nie podano numeru przesyłki");
				}
				else{
					errorInfo.setVisible(false);
					try {
						parcel = null;
						parcel = db.getParcelById(Integer.parseInt(deliveryInput.getText()));
						deliveryDetails.setText("Szczegóły przesyłki: \n Data nadania:\t"+parcel.getDate()+
								"\n Cena:\t"+String.valueOf(parcel.getPrice())+"\n Typ:\t"+parcel.getType());
						deliveryDetails.setVisible(true);
						parcelStatusList.setSelectedItem(db.getStatus(parcel.getStatus()));
						parcelStatusList.setVisible(true);
						statusInfo.setVisible(true);
						confirm.setVisible(true);
						repaint();
						validate();
					} catch (NumberFormatException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						errorInfo.setText("Nie podano numeru przesyłki");
						errorInfo.setVisible(true);
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						errorInfo.setText("Nie podano numeru przesyłki");
						errorInfo.setVisible(true);
					} catch (NullPointerException e1){
						errorInfo.setText(e1.getMessage());
						errorInfo.setVisible(true);
						deliveryDetails.setVisible(false);
						parcelStatusList.setVisible(false);
						statusInfo.setVisible(false);
						confirm.setVisible(false);
						repaint();
						validate();
					}
				}
			}
		});
		
		confirm.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				db.modifyParcelStatus(parcel.getParcelID(),parcelStatusList.getSelectedIndex()+1);
				try {
					worker.statusOfDeliveryHasChanged(db.getClientsByField("id", String.valueOf(parcel.getRecipient())).get(0).getEmail(), String.valueOf(parcel.getParcelID()), parcelStatusList.getSelectedItem().toString());
					worker.statusOfDeliveryHasChanged(worker.getUserById(parcel.getSender()).geteMail(), String.valueOf(parcel.getParcelID()), parcelStatusList.getSelectedItem().toString());
					errorInfo.setVisible(true);
					errorInfo.setText("Zmieniono status, wysłano wiadomość email do odbiorcy");
				} catch (AddressException e) {
					errorInfo.setVisible(true);
					errorInfo.setText("Status zaktualizowany. Brak podanego adresu email w bazie");
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					errorInfo.setVisible(true);
					errorInfo.setText("Status zaktualizowany. Serwer poczty nie odpowiada. Mail nie został wysłany");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	Worker worker;
	private Communication db;
	private WorkerHomePage parent;
	private Parcel parcel;
	private JLabel statusInfo;
	private JLabel checkDeliveryInfo;
	private JLabel info;
	private JLabel errorInfo;
	private JComboBox parcelStatusList;
	private JTextField deliveryInput;
	private JButton searchButton;
	private JTextArea deliveryDetails;
	private JButton confirm;
}
